﻿using BaseData.Domain;
using BaseData.Infrastructure.DTO;
using BaseData.Infrastructure.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace BaseData.Infrastructure.Permission
{
    public class PermissionsService : IPermissionsService
    {
        private readonly IBaseRepository<Permissions> perRep;
        private readonly IBaseRepository<RolePermissions> roleperRep;

        public PermissionsService(IBaseRepository<Permissions> perRep,IBaseRepository<RolePermissions> roleperRep)
        {
            this.perRep = perRep;
            this.roleperRep = roleperRep;
        }
        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="permissionIds"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddRolePermission(AddRolePermissionDto dto)
        {
            //foreach(var item in dto.permissionIds)
            //{
            //    var roleper = new RolePermissions();
            //    roleper.RoleId = dto.roleId;
            //    roleper.PermissionId = item;
            //    await roleperRep.AddAsync(roleper);
            //}
            //return 1;
            //创建事务
            using (var tran = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    //根据角色编号查权限
                    var permissionIdlist = await roleperRep.GetAlls(x => x.RoleId == dto.roleId).Select(x => x.PermissionId).ToListAsync();
                    //删除原有权限
                    foreach (var item in permissionIdlist)
                    {
                        var roleperId = await roleperRep.GetAlls(x => x.RoleId == dto.roleId && x.PermissionId == item).Select(x => x.RoPeId).FirstOrDefaultAsync();
                        await roleperRep.DelAsync(roleperId);
                    }
                    //完成角色权限编号保存
                    foreach (var item in dto.permissionIds)
                    {
                        var roleper = new RolePermissions();
                        roleper.RoleId = dto.roleId;
                        roleper.PermissionId = item;
                        await roleperRep.AddAsync(roleper);
                    }
                    tran.Complete();
                    return 1;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }

        /// <summary>
        /// 显示权限树
        /// </summary>
        /// <param name="parentId">父级编号</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<PermissionTree>> GetPermissionTrees(int parentId)
        {
            var res = perRep.GetAlls(x => x.ParentId == parentId).ToList();
            List<PermissionTree> list = new List<PermissionTree>();
            foreach (var item in res)
            {
                PermissionTree tree = new PermissionTree();
                tree.id = item.PermissionId;
                tree.label = item.PermissionName;
                tree.children = await GetPermissionTrees(item.PermissionId);
                list.Add(tree);
            }
            return list;
        }

        /// <summary>
        /// 根据角色id获取权限
        /// </summary>
        public async Task<List<int>> GetPermissionByRole(int roleId)
        {
            var res = await roleperRep.GetAlls(x => x.RoleId == roleId).ToListAsync();
            var permissionList = res.Select(x => x.PermissionId).ToList();
            return permissionList;
        }
    }
}
